package org.example.utils;


import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 李国树
 * @version 1.0
 * @Desc
 * @date 2023/7/28 10:08
 */
public class VerifyIdUtils {
    public static void main(String[] args) {
        boolean verify = verify("李国树", "51132319860311337X");
        System.out.println(verify);
    }
    public static boolean verify(String name,String idNumber) {
        String host = "https://dfidveri.market.alicloudapi.com";
        String path = "/verify_id_name";
        String method = "POST";
        String appcode = "b30ebc64e4204432afe83d478e86b786";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("id_number", idNumber);
        bodys.put("name", name);
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            VerifyResult verifyResult = JSONUtil.toBean(EntityUtils.toString(response.getEntity()), VerifyResult.class);
            System.out.println(verifyResult);
            if(verifyResult.getState()==1){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
@Data
class VerifyResult{
    private String status;
    private Integer state;
    private String result_message;
    private String request_id;
}


